﻿using System.Collections.Generic;
using Demo.Api.Entities;
using Microsoft.EntityFrameworkCore;

namespace Demo.Api.Data
{
    /// <summary>
    ///     数据库上下文
    /// </summary>
    public class DemoDbContext : DbContext
    {
        /// <summary>
        ///     构造方法
        /// </summary>
        /// <param name="options"></param>
        public DemoDbContext(DbContextOptions<DemoDbContext> options) : base(options)
        {
        }

        /// <summary>
        ///     事项
        /// </summary>
        public DbSet<AffairDo> Affairs { get; set; }

        /// <summary>
        ///     事项实例
        /// </summary>
        public DbSet<AffairObjectDo> AffairObjects { get; set; }

        /// <summary>
        ///     流程图实例
        /// </summary>
        public DbSet<FlowChartProcessDo> FlowChartProcesses { get; set; }
        /// <summary>
        ///     走过的线
        /// </summary>
        public DbSet<ProcessLineDo> ProcessLines { get; set; }
        /// <summary>
        ///     流程图
        /// </summary>
        public DbSet<FlowChartDo> FlowCharts { get; set; }

        /// <summary>
        ///     线
        /// </summary>
        public DbSet<LineDo> TabLines { get; set; }

        /// <summary>
        ///     节点
        /// </summary>
        public DbSet<NodeDo> Nodes { get; set; }

        /// <summary>
        ///     反向连线时，清空的节点
        /// </summary>
        public DbSet<ReverseClearLineAndNodeDo> ReverseClearLineAndNodes { get; set; }

        /// <summary>
        ///     创建Model
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AffairDo>(m =>
            {
                m.ToTable("tab_affair");
                m.HasKey(x => x.Id);
                m.Property(x => x.Id).ValueGeneratedNever();
                m.Property(x => x.Name).IsRequired().HasMaxLength(1023);
            });
            modelBuilder.Entity<AffairObjectDo>(m =>
            {
                m.ToTable("tab_affair_object");
                m.HasKey(x => x.Id);
                m.Property(x => x.Id).ValueGeneratedNever();
            });
            modelBuilder.Entity<FlowChartDo>(m =>
            {
                m.ToTable("tab_flow_chart");
                m.HasKey(x => x.Id);
                m.Property(x => x.Id).ValueGeneratedNever();
                m.HasData(new FlowChartDo
                {
                    Id = 1,
                    Name = "第一个流程"
                });
            });
            modelBuilder.Entity<FlowChartProcessDo>(m =>
            {
                m.ToTable("tab_flow_chart_process");
                m.HasKey(x => x.Id);
                m.Property(x => x.Id).ValueGeneratedNever();
            });
            modelBuilder.Entity<ProcessLineDo>(m =>
            {
                m.ToTable("tab_process_line");
                m.HasKey(x => x.Id);
                m.Property(x => x.Id).ValueGeneratedNever();
            });
            modelBuilder.Entity<LineDo>(m =>
            {
                m.ToTable("tab_line");
                m.HasKey(x => x.Id);
                m.Property(x => x.Id).ValueGeneratedNever();
            });
            modelBuilder.Entity<ReverseClearLineAndNodeDo>(m =>
            {
                m.ToTable("tab_reverse_clear_line_node");
                m.HasKey(k => k.LineId);
                m.HasKey(k => k.NodeId);
            });
            modelBuilder.Entity<NodeDo>(m =>
            {
                m.ToTable("tab_node");
                m.HasKey(x => x.Id);
                m.Property(x => x.Id).ValueGeneratedNever();
                m.HasData(new List<NodeDo>
                {
                    new NodeDo
                    {
                        Id = 1, Name = "开始", FlowChartId = 1, Logic = 1, NodeType = 1, SeparateNodeId = 0,
                        OrClear = false, RuleId = 0
                    }
                });
            });
        }
    }
}